<?php
/**
 * Goods.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/12/9- 下午4:05
 */

namespace common\components\api\isv;

use common\exception\ServicesException;
use yii\helpers\FileHelper;
use Yii;


class Goods extends BaseIsv
{

    /**
     * 重试3次
     * @var int
     */
    public $retryTimes = 3;

    /**
     * 商品标准类目接口
     * @param $catId
     * @param int $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsCatsGet($catId = 0, int $mall_id = null)
    {
        $action = 'pdd.goods.cats.get';

        $args = [
            ['name' => 'parent_cat_id', 'contents' => $catId]
        ];
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $args, $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }

        if (!$resData) {
            return [];
        }
        return $resData['goods_cats_get_response'];

    }

    /**
     * 获取商品提交的商品详情
     * @param int $goodsCommitId
     * @param int $goodsId
     * @param int $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsCommitDetailGet(int $goodsCommitId, int $goodsId, int $mall_id = null)
    {
        $action = 'pdd.goods.commit.detail.get';

        $args = [
            ['name' => 'goods_commit_id', 'contents' => $goodsCommitId],
            ['name' => 'goods_id', 'contents' => $goodsId],
        ];
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $args, $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }
        return $resData['goods_commit_detail_response'];
    }

    /**
     * 获取商品提交的商品详情
     * @param int $goodsCommitId
     * @param int $goodsId
     * @param int $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsSetRelation(int $goodsid, int $out_goods_id, $out_detail_url, int $out_low_goods_price, int $out_high_goods_price, int $postage, $source_type = 0, int $mall_id = 0)
    {
        $action = 'pdd.goods.relation.set';

        $args = [
            ['name' => 'goods_id', 'contents' => $goodsid],
            ['name' => 'out_goods_id', 'contents' => $out_goods_id],
            ['name' => 'out_detail_url', 'contents' => $out_detail_url],
            ['name' => 'out_low_goods_price', 'contents' => $out_low_goods_price],
            ['name' => 'out_high_goods_price', 'contents' => $out_high_goods_price],
            ['name' => 'postage', 'contents' => $postage],
            ['name' => 'source_type', 'contents' => $source_type],
        ];

        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $args, $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }
        return $resData['goods_relation_set_response'];
    }

    /**
     * 商品新增接口
     * @param array $args
     * @param int $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsAdd(array $args, int $mall_id = null)
    {
        $action = 'pdd.goods.add';

        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $this->formatArgs($args), $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }
        if (!$resData) {
            return [];
        }
        return $resData;
//        return $resData['goods_add_response'];
    }

    /**
     * 商品编辑接口
     * @param array $args
     * @param int $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsUpdate(array $args, int $mall_id = null)
    {
        $action = 'pdd.goods.information.update';
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $this->formatArgs($args), $mall_id, 1);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }
        if (!$resData) {
            return [];
        }
        return $resData['goods_update_response'];
    }

    /**
     * 城市代码获取
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsCountry(int $mall_id = null)
    {
        $action = 'pdd.goods.country.get';
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, array(), $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }
        if (!$resData) {
            return [];
        }
        return $resData['goods_country_get_response'];
    }

    /**
     * 批量删除商品接口
     * @param $goodsId
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsDelete($goodsId, int $mall_id = null)
    {
        $action = 'pdd.delete.goods.commit';
        $args = ['goods_ids' => $goodsId];
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $this->formatArgs($args), $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }

        if (!$resData) {
            return [];
        }
        return $resData;
    }


    /**
     * 草稿箱
     * @param array $args
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsEditGoodsCommit(array $args, int $mall_id = null)
    {
        $action = 'pdd.goods.edit.goods.commit';

        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $this->formatArgs($args), $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }
        if (!$resData) {
            return [];
        }
        return $resData;
//        return $resData['goods_update_response'];
    }


    /**
     * 商品上架状态设置
     * @param int $goodsId
     * @param int $isOnsale
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsSaleStatusSet(int $goodsId, int $isOnsale, int $mall_id = null)
    {
        $action = 'pdd.goods.sale.status.set';
        $args = [
            ['name' => 'goods_id', 'contents' => $goodsId],
            ['name' => 'is_onsale', 'contents' => $isOnsale],
        ];
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $args, $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }
        if (!$resData) {
            return [];
        }
        return $resData['goods_sale_status_set_response'];

    }


    /**
     * 商品类目预测接口
     * @param array $args
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsOuterCatMappingGet(array $args = [], int $mall_id = null)
    {
        $action = 'pdd.goods.outer.cat.mapping.get';

        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $this->formatArgs($args), $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }
        if (!$resData) {
            return [];
        }
        return $resData['outer_cat_mapping_get_response'];
    }


    /**
     * 商品运费模版接口
     * @param array $args
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsLogisticsTemplateGet(array $args = [], int $mall_id = null)
    {
        $action = 'pdd.goods.logistics.template.get';
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $this->formatArgs($args), $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }

        if (!$resData) {
            return [];
        }
        return $resData['goods_logistics_template_get_response'];
    }

    /**
     *
     * 商品列表接口
     * @param array $args
     * @param int|null $mall_id
     * @return array|mixed
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsListGet(array $args = [], int $mall_id = null)
    {
        $action = 'pdd.goods.list.get';
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $this->formatArgs($args), $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }

        if (!$resData) {
            return [];
        }
        return $resData['goods_list_get_response'];
    }


    /**
     * 商品明细
     * @param $goodsId
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsDetailGet($goodsId, int $mall_id = null)
    {
        $action = 'pdd.goods.detail.get';
        $args = [
            ['name' => 'goods_id', 'contents' => $goodsId]
        ];
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $args, $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }


        if (!$resData) {
            return [];
        }
        return $resData['goods_detail_get_response'];
    }


    /**
     * 商品详情接口
     * @param $goodsId
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsInformationGet($goodsId, int $mall_id = null)
    {
        $action = 'pdd.goods.information.get';
        $args = [
            ['name' => 'goods_id', 'contents' => $goodsId]
        ];
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $args, $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }

        if (!$resData) {
            return [];
        }
        return $resData['goods_info_get_response'];
    }


    /**
     * 商品国家接口
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsCountryGet(int $mall_id = null)
    {
        $action = 'pdd.goods.country.get';
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, [], $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }

        if (!$resData) {
            return [];
        }
        return $resData['goods_country_get_response'];
    }


    /**
     * 商品图片上传接口
     * @param string $imgBase64
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsImageUpload(string $file, int $mall_id = null)
    {
        $action = 'pdd.goods.img.upload';
        $signData = $this->sign($action,$mall_id);
        if(!$signData){
            return [];
        }
        if(!$signData['sign']){
            return [];
        }
        $args = [
            'file' => file_get_contents($file),
            'sign' => $signData['sign'],
            'client_id' => $signData['client_id'],
            'timestamp' => $signData['timestamp'],
            'access_token' => $signData['access_token'],
            'type' => $action
        ];
        $resData = $this->requestPdd($action, $args, $mall_id);

        return $resData['goods_img_upload_response']['url'];
    }

    /**
     * token接口
     * @param string $imgBase64
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsRefresh(string $token, int $mall_id = null)
    {
        $action = 'pdd.pop.auth.token.refresh';

        $tryTimes = 0;
        $arg = array(
            'refresh_token' => $token
        );
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestPddToken($action, $arg, $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }

        return $resData;

    }




    /**
     * 获取当前授权商家可发布的商品类目信息
     * @param int $parent_cat_id
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsAuthorizationGet(int $parent_cat_id, int $mall_id = null)
    {
        $action = 'pdd.goods.authorization.cats';
        $args = [
            ['name' => 'parent_cat_id', 'contents' => $parent_cat_id]
        ];
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $args, $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }

        if (!$resData) {
            return [];
        }
        return $resData['goods_auth_cats_get_response'];
    }


    /**
     * 获取商品类目属性
     * @param int $cidId
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsCatTemplateGet(int $cidId, int $mall_id = null)
    {
        $action = 'pdd.goods.cat.template.get';

        $args = [
            ['name' => 'cat_id', 'contents' => $cidId]
        ];
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $args, $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }


        if (!$resData) {
            return [];
        }
        if (!isset($resData['open_api_response'])) {
            return [];
        }
        return $resData['open_api_response'];
    }


    /**
     * 生成商家自定义的规格
     * @param int $parent_spec_id
     * @param string $spec_name
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsSpecIdGet(int $parent_spec_id, string $spec_name, int $mall_id = null)
    {
        $action = 'pdd.goods.spec.id.get';
        $args = [
            ['name' => 'parent_spec_id', 'contents' => $parent_spec_id],
            ['name' => 'spec_name', 'contents' => $spec_name],
        ];
        $resData = $this->requestIsv($action, $args, $mall_id);

        if (!$resData) {
            return [];
        }
        if (isset($resData['goods_spec_id_get_response'])) {
            return $resData['goods_spec_id_get_response'];
        }
        if (isset($resData['error_response'])) {
            return $resData['error_response'];
        }

    }


    /**
     * 商品属性类目接口
     * @desc 平台即将废弃，慎用
     * @param int $cidId
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsSpecGet(int $cidId, int $mall_id = null)
    {
        $action = 'pdd.goods.spec.get';

        $args = [
            ['name' => 'cat_id', 'contents' => $cidId]
        ];
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $args, $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }

        if (!$resData) {
            return [];
        }
        return $resData['goods_spec_get_response'];
    }


    /**
     * 站内外属性映射接口
     * @param array $args
     * @param int|null $mall_id
     * @return array
     * @throws \common\exception\ServicesException
     */
    public function pddGoodsOutPropertyMappingGet(array $args, int $mall_id = null)
    {
        $action = 'pdd.goods.out.property.mapping.get';
        $tryTimes = 0;
        while ($tryTimes <= $this->retryTimes) {
            $resData = $this->requestIsv($action, $this->formatArgs($args), $mall_id);
            if ($resData) {
                break;
            } else {
                $tryTimes++;
                sleep(1);
            }
        }

        if (!$resData) {
            return [];
        }
        return $resData['out_property_mapping_get_response'];
    }

    /**
     * 文件日志记录
     * @param $msg
     * @throws ServicesException
     */
    public function writeSpiderLog($msg)
    {

        $path = Yii::getAlias('@runtime/api/apipicture/');

        //目录如果不存就先创建目录
        if (!is_dir($path)) {
            FileHelper::createDirectory($path);
        }

        $cp = fopen($path . 'log-' . date('Ymd H:i') . '.log', "a+");
        $txt = "【" . date('Y-m-d H:i:s', time()) . "】---" . $msg . "\r\n";
        fwrite($cp, $txt);
        fclose($cp);

    }



}